/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.eurotrader.navegacao;

import br.com.eurotrader.utils.FacesUtils;
import br.com.eurotrader.beanssessao.ListasTabelas;
import br.com.eurotrader.utils.MetodosBotoes;
import br.com.eurotrader.beanssessao.SessionBean1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;

/**
 *
 * @author PROG01
 */
@Named(value="navegaEntrePaginas")
public class NavegarPorPaginas {

    public void navegarPagina(ActionEvent event) {
        ((MetodosBotoes) FacesUtils.getManagedBean("botoes")).getControllerProximaPagina(event.getComponent().getId()).abrirPagina();
        ((MetodosBotoes) FacesUtils.getManagedBean("botoes")).getControllerProximaPagina(event.getComponent().getId()).inserir(event);
        ArrayList<String> lista = ((SessionBean1)((SessionBean1)FacesUtils.getManagedBean("sessionBean1"))).getListaDePaginasNavegadas();
        if (lista == null) {
            lista = new ArrayList<String>();
        }
        lista.add(FacesContext.getCurrentInstance().getViewRoot().getViewId().toString().substring(1, FacesContext.getCurrentInstance().getViewRoot().getViewId().toString().indexOf(".")));
        FacesContext context = FacesContext.getCurrentInstance();
        context.getApplication().getNavigationHandler().handleNavigation(context, null, event.getComponent().getId());
        ((SessionBean1)FacesUtils.getManagedBean("sessionBean1")).setListaDePaginasNavegadas(lista);
        ((ListasTabelas) FacesUtils.getManagedBean("listasTabelas")).limparListas();
    }

    public void voltarPagina() {
        ArrayList<String> lista = ((SessionBean1)FacesUtils.getManagedBean("sessionBean1")).getListaDePaginasNavegadas();
        if (lista != null) {
            String nomePagina = lista.get(lista.size() - 1);
            FacesContext context = FacesContext.getCurrentInstance();
            context.getApplication().getNavigationHandler().handleNavigation(context, null, nomePagina);
            lista.remove(nomePagina);
            if (lista.isEmpty()) {
                lista = null;
            }
            ((SessionBean1)FacesUtils.getManagedBean("sessionBean1")).setListaDePaginasNavegadas(lista);
            ((ListasTabelas) FacesUtils.getManagedBean("listasTabelas")).limparListas();
        }
    }
    
    
    public void navegarPlataforma(ActionEvent event) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("plataforma.jsf");
        } catch (IOException ex) {
            Logger.getLogger(NavegarPorPaginas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
